package com.ztjava.common;

import org.apache.log4j.Logger;

import com.jfinal.aop.Duang;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.template.Engine;
import com.ztjava.common.model.flow.FlowInfo;
import com.ztjava.common.service.FlowService;
import com.ztjava.common.service.PushService;
import com.ztjava.common.service.TreeService;
import com.ztjava.common.tool.files.QiniuTool;
import com.ztjava.core.Constant;
import com.ztjava.core.Jsub;
import com.ztjava.core.base.BaseSub;
import com.ztjava.core.tool.ConfigTool;
import com.ztjava.core.tool.ViewTool;

public class Common extends BaseSub{
	private static Logger logger = Logger.getLogger(Common.class);

	public static final String local = "local";
	public static final String qiniu = "qiniu";
	public static final String COMMON_FILES_TYPE = "COMMON_FILES_TYPE";
	
	public static final String COMMON_FILES_QINIU_ACCESSKEY = "COMMON_FILES_QINIU_ACCESSKEY";
	public static final String COMMON_FILES_QINIU_SECRETKEY = "COMMON_FILES_QINIU_SECRETKEY";
	public static final String COMMON_FILES_QINIU_BUCKET = "COMMON_FILES_QINIU_BUCKET";
	public static final String COMMON_FILES_QINIU_CDN = "COMMON_FILES_QINIU_CDN";
	
	@Override
	public void configConstant(Constants me) {
		
		
	}

	@Override
	public void configRoute(Routes me) {
		
		
	}

	@Override
	public void configEngine(Engine me) {
		
		
	}

	@Override
	public void configPlugin(Plugins me) {
		
		
	}

	@Override
	public void configInterceptor(Interceptors me) {
		
		
	}

	@Override
	public void configHandler(Handlers me) {
		
		
	}

	@Override
	public void afterJFinalStart() {
		Cache cache = Redis.use();
		if(Jsub.dev||!cache.exists(Constant.R_PRE+"COMMON_PUSH_TEMPLATE")){
			PushService pushService = Duang.duang(PushService.class);
			pushService.loadTemplateCache();
		}

		if(Jsub.dev||!cache.exists(Constant.R_TREE)){
			TreeService treeService = Duang.duang(TreeService.class);
			treeService.loadInfoCache();
		}
		if(Jsub.dev||!cache.exists(Constant.R_FLOW)){
			FlowService flowService = Duang.duang(FlowService.class);
			flowService.loadInfoCache();

			ConfigTool.dbToBaseData("COMMON_FLOW", "select code as value,name,enable_flag from "
					+ FlowInfo.tablename + " order by id asc");
		}
	}

	@Override
	public void startComplete() {
		String type = ViewTool.param(COMMON_FILES_TYPE,local);
		if(type.equals(qiniu)&&!QiniuTool.initCheck()){
			logger.error("Common初始化检查未通过！");
			throw new RuntimeException("Common初始化检查未通过！");
		}
	}

}
